//package com.mk.api.open.config;
//
//import com.alibaba.fastjson.JSON;
//import com.mk.api.open.domain.MtResult;
//import com.sankuai.sjst.platform.developer.utils.SignUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.HashMap;
//import java.util.Iterator;
//import java.util.Map;
//import java.util.Objects;
//
///**
// * 〈美团外卖拦截器〉
// *
// * @author renqingqing
// * @create 2020/10/20
// */
//@Component
//public class MtHandlerInterceptor implements HandlerInterceptor {
//
//    private static final String SIGN = "sign";
//    @Value("${meituan.waimai.secret}")
//    private String secret;
//
//    //@Value("${spring.profiles.active}")
//    //private String profileActive;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        /**
//         * 心跳检测时会发送空包，能对空包做处理
//         * 根据sign判断有没有传数据
//         */
//        String sign = request.getParameter(SIGN);
//        if (StringUtils.isEmpty(sign)) {
//            returnMsg(response);
//            return false;
//        } //预发环境不验证签名
//        //if ("pre".equals(profileActive)) {
//        //    return true;
//        //}
//        Map<String, String> paramsMap = new HashMap<>();
//        // 获取参数
//        Map<String, String[]> map = request.getParameterMap();
//        for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
//            Map.Entry entry = (Map.Entry) it.next();
//            String key = (String) entry.getKey();
//            String[] value = (String[]) entry.getValue();
//            if (Objects.equals(SIGN, key)) {
//                continue;
//            }
//            paramsMap.put(key, value[0]);
//        }
//        // 计算秘钥
//        String selfSign = SignUtils.createSign(secret, paramsMap);
//        if (!Objects.equals(sign, selfSign)) {
//            System.out.println("签名校验失败 sign="+sign+", selfSign="+selfSign+", 参数="+map);
//            returnMsg(response);
//            return false;
//        }
//        return true;
//    }
//
//    public void returnMsg(HttpServletResponse response) throws IOException {
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
//        PrintWriter writer = response.getWriter();
//        MtResult result = MtResult.success();
//        writer.println(JSON.toJSONString(result));
//    }
//}
